import { login, logout, getInfo } from '@/api/app'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { sha256 } from 'js-sha256'
import { encode } from 'js-base64'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    displayName: '',
    avatar: '',
    id: -1,
    introduction: '',
    roles: [],
    permissions: [],
    processKey:'',
    router:''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_DISPLAY_NAME: (state, displayName) => {
    state.displayName = displayName
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLE: (state, roles) => {
    state.roles = roles;
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions;
  },
  SET_ID: (state, id) => {
    state.id = id;
  },
  SET_PROCESSKEY: (state, processKey) => {
    state.processKey = processKey;
  },
  SET_ROUTER: (state, router) => {
    state.router = router;
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    let { username, password } = userInfo
    password = sha256(password);
    const header = encode(JSON.stringify({
      username: username.trim(),
      password: password,
    }))
    return new Promise((resolve, reject) => {
      login({ header }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        commit('SET_ROUTER', data.router)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar, roles, permissions, id,displayName,processKey,router } = data

        commit('SET_PERMISSIONS', permissions)
        commit('SET_ID', id)
        commit('SET_ROLE', roles)
        commit('SET_NAME', name)
        commit('SET_DISPLAY_NAME', displayName)
        commit('SET_AVATAR', avatar)
        commit('SET_PROCESSKEY', processKey)
        commit('SET_ROUTER', router)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

